<?php
$host = 'http://'.$_SERVER['HTTP_HOST'];
$directory = dirname(dirname($_SERVER['SCRIPT_NAME']));
$root = $directory == '\\' ? $host.'/' : $host.$directory.'/';
define(VIEW_PATH, $root.'views/');
define(ROOT_PATH, $root);

class PagingBook{
    private $db;
    private $page;
    
    function __construct()
    {
        $this->page = 0;
        $this->db = new DB();
        $this->statisticURL = $this->db->get('configManager')->statisticURL;
        
        $this->db->startConnection();
    }
    function __destruct()
    {
        $this->db->closeConnection();
    }
    public function getPluginContent($pluginResult){
		$like = ($pluginResult['like'] != 0 ? $pluginResult['like'] : 0);
		$share = ($pluginResult['share'] != 0 ? $pluginResult['share'] : 0);
		$retweet = ($pluginResult['retweet'] != 0 ? $pluginResult['retweet'] : 0);
		
		$result = '<div class="eachPlugin" id="like"></div>		<div class="number">' . $like . '</div>'.
				  '     <div class="eachPlugin" id="share"></div>  		<div class="number">' . $share . '</div>'.
				  '     <div class="eachPlugin" id="retweet"></div>		<div class="number">' . $retweet . '</div>';
		return $result;
	}
    public function run($action, $page, $hash){
     
        $this->page = $page;
        $str ='';
        $num = 0;
        $showedBooks = 0;
        $numOfBook = 0;
        $numOfPage = 0;
        
        if($action == 'book'){
            $num = $this->db->get('configManager')->getById(2)->get("value");
            $showedBooks = $this->db->get('bookManager')->getAllCategoryBooks(($page-1)*$num, $num);
            $numOfBook =  $this->db->get('bookManager')->getAllBooks(); 
            $numOfPage = ceil($numOfBook / $num);
            
            if($page > $numOfPage){
                $str .= "Page not found!";
                return $str;
            }
            $maxdate = $this->db->get('bookManager')->getMaxDate();          
            foreach($showedBooks as $book){
                $thumbFile = dirname(dirname($_SERVER['SCRIPT_FILENAME'])).'/views/images/thumbnail/'. $book->get("thumbnail");
        		
                $thumb = '';
                if(!file_exists($thumbFile)){            
                    $thumb = 'images.jpg';                  
                }
                else {
                    $thumb = $book->get("thumbnail");
                   
                //echo ROOT_PATH;
                //exit();
                }
                $attTag == '' ;
    			if($book->get('special') == 1) {
        			$attTag = '<span class="special_icon"><img src="'.VIEW_PATH.'images/special_icon.gif" alt="" title="" /></span>';
        		} else if (strtotime($book->get('created')) > strtotime($maxdate) -7*24*60*60)
        			$attTag = '<span class="special_icon"><img src="'.VIEW_PATH.'images/new_icon.gif" alt="" title="" /></span>';
               		
              
                $str .= '<div class="new_prod_box">
                	<a class="ellipsis" id="ellipsis" title="'.$book->get("title").'" href="'.ROOT_PATH.'detail/'.$book->get("nid").'">'.$book->get("title").'</a>	
                	<div class="new_prod_bg">'
                		.$attTag.
                		'<a href="'.ROOT_PATH.'detail/'.$book->getURLRewrite().'" class="preview" title="'.$book->get("title").'">
                			<img src="'.VIEW_PATH.'images/thumbnail/'.$thumb.'" alt="" class="thumb" border="0" /> 
                		</a>
                	</div>
                </div>';    
                
            }                         
        }else if($action == 'discount'){
            
            $num = $this->db->get('configManager')->getById(1)->get("value"); //get special page size
           	$showedBooks = $this->db->get('bookManager')->getDiscountBooks(($this->page-1)*$num, $num);   	 	 	 
            $numOfBook = $this->db->get('bookManager')->getNumberOfDiscountBooks();
            $numOfPage = ceil($numOfBook/$num);
            
            if($page > $numOfPage){
                $str .= "Page not found!";
                return $str;
            }           
            foreach($showedBooks as $book){
                $catMan = $this->db->get('catManager');
                $maxdate = $this->db->get('bookManager')->getMaxDate();
                $percent = $this->db->get('bookManager')->getPercent($book->get('discount'));
        		if ($attTag == '') {
        			$attTag = '<span class="special_icon"><img class="yellow_label" src="'.VIEW_PATH.'images/label_sale yellow.png"  alt="" title="" /><span class="percent">'.$percent.'%</span></span>';
        		}
        		
        		$plugin = $this->db->get('bookManager')->getPlugin($book->get('nid'));
                
                $thumbFile = dirname(dirname($_SERVER['SCRIPT_FILENAME'])).'/views/images/thumbnail/'. $book->get("thumbnail");
        		
                $thumb = '';
                if(!file_exists($thumbFile)){            
                    $thumb = 'images.jpg';
                }
                else {
                    $thumb = $book->get("thumbnail");
                }
                $str .='<div class="feat_prod_box">'.
                    	'<div class="prod_img">'.
                    		'<a href="'.ROOT_PATH.'detail/'.$book->getURLRewrite().'"><img
                    			src="'.VIEW_PATH.'images/thumbnail/'.$thumb.'" alt="" title="" border="0" class="size150"/>
                    		</a>
                    	</div>
                    	<div class="prod_det_box">'
                    		.$attTag.
                    		'<div class="box_top"></div>
                    		<div class="box_center">
                    			<div class="details">
									<p>Author: &nbsp'. $book->get("title") .'</p>
									<p>Translator: &nbsp'. $book->get("translator").'</p>
									
								</div> 
                    			<a href="'.ROOT_PATH.'detail/'.$book->getURLRewrite().'" class="more">- more details -</a>
                    			<div class="clear"></div>
                    		</div>
                    		<div class="box_bottom"></div>
                    	</div>
                    	<div class="clear"></div>
                    </div>';

            }
                 
        }else {
            return "ErrorController";
        }
        if(isset($hash)){
            if($numOfBook == 0 || $numOfPage ==1 ){
                $str .= '<div class="pagination"> </div>';
            }else {
               if ($this->page == 1){
                    $str .='<div class="pagination">' ;
                    $i = 0;
                    for($i=$this->page ; $i < $this->page + 3; $i++){
                        if($i <= $numOfPage){
                            if($i == $this->page){
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '"><span class="current">' .$i. '</span></a>';
                            }else{
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '">' .$i. '</a>';    
                            }
                                
                        }            
                    }
                    if($i < $numOfPage){
                        $str .= "...";
                    }
                    $next = $this->page + 1;
                    $str .= '<a href="'.ROOT_PATH.$action.'/' .$next . '"> &gt;&gt; </a>';
                    $str .= '<a href="'.ROOT_PATH.$action.'/' .$numOfPage . '"> [Last] </a>';
                    $str .= '</div>';
            		            
                } else if ($this->page == $numOfPage){
                    $previous = $this->page - 1;
                    $str .='<div class="pagination"> <a href="'.ROOT_PATH.$action.'/1">[First]</a>';
                    $str .='<a href="'.ROOT_PATH.$action.'/' .$previous.'">&lt;&lt;</a>';
                    $i = $this->page - 2;
                    if($i > 1){
                        $str .= "...";
                    }
                    for($i=$this->page - 2; $i <= $numOfPage; $i++){
                        if($i >= 1 && $i <= $numOfPage){
                            if($i == $this->page){
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '"><span class="current">' .$i. '</span></a>';
                            }else{
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '">' .$i. '</a>';    
                            }                    
                        }            
                    }    
                   
                    $str .= '</div>';
                           
                } else {
                    $previous = $this->page - 1;
                    $next = $this->page + 1;
                    $str .='<div class="pagination"> <a href="'.ROOT_PATH.$action.'/1">[First]</a>';
                    $str .='<a href="'.ROOT_PATH.$action.'/' .$previous. '">&lt;&lt;</a>';
                    $i = $this->page;
                    for($i = $this->page - 1 ; $i < $this->page + 2; $i++){
                        if($i <= $numOfPage){
                            if($i == $this->page){
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '"><span class="current">' .$i. '</span></a>';
                            }else{
                                $str .= '<a href="'.ROOT_PATH.$action.'/' .$i. '">' .$i. '</a>';    
                            }    
                        }
                            
                    }
                    if($i < $numOfPage){
                        $str .= "...";
                    }
                    $str .= '<a href="'.ROOT_PATH.$action.'/' .$next. '"> &gt;&gt; </a>';
                    $str .= '<a href="'.ROOT_PATH.$action.'/' .$numOfPage . '"> [Last] </a>';
                    $str .= '</div>';
               }
                $str .='<script type="text/javascript">'.
                            '$(document).ready(function(){'.
                                 '$(".new_products .pagination a").click(pageClickHandle);'.  
                            '});'.
                        '</script>';                    
            }
        }
       return $str;
     
    }
   
}
?>